package tuiLayer;
import modelLayer.*;
import controlLayer.*;
import java.util.Scanner;

public class CustomerUI{
    Scanner keyboard = new Scanner(System.in);
    CustomerController customerController=new CustomerController();;
    String errorMessage = "Incorrect command!";
    String username;
    String name;
    String address;
    String phoneNo;
    int cprNo = 0;
    private boolean isCraftsman;
    MainUI mainUI;
    
    public CustomerUI(){
        writeCustomerMenu(); 
    }
    
    public void writeCustomerMenu(){
        System.out.println("\f***Start customer menu***");
        System.out.println(" (1) Create customer");
        System.out.println(" (2) Find customer");
        System.out.println(" (3) Update customer");
        System.out.println(" (4) Promote customer to regular customer");
        /*
        System.out.println(" *(5) Delete customer* (To be implemented)");
        System.out.println(" *(6) Update Customers* CPR number (To be implemented)");
        */
        System.out.println(" (7) Go back to main menu");
        
        String choice = getString();
        
        switch(choice){
            case "1":
               makeCustomer();
               break;
            case "2":
                findCustomer();
                break;
            case "3":
                updateCustomer();
                break;
            case "4":
                updateCustomerToRegular();
                break;
            //case "5":
                //deleteCustomer();
                //break;
            //case "6":
                //updateCpr();
                //break;
            case "7":
                mainUI = new MainUI();
                mainUI.writeMainMenu();
                break;
            default:      
                System.out.println(errorMessage);
                pressEnter();
                writeCustomerMenu();
        }
    }
    private void makeCustomer(){
        System.out.println("\fEnter CPR number (without - between numbers): ");
        String cprNoTemp = getString();
        cprNo = Integer.parseInt(cprNoTemp);
        if(completeCpr() == true){
            findCustomer();
        }
        nextStep();
    }
    private void nextStep(){
        enterName();
        enterAddress();
        enterPhoneNo();
        enterIsCraftsman();
    }
    private void enterName(){
        System.out.println("\fEnter name: ");
        name = getStringForNames();
        enterAddress();
    }
    private void enterAddress(){
        System.out.println("\fEnter address: ");
        address = getStringForNames();
        enterPhoneNo();
    }
    private void enterPhoneNo(){
        System.out.println("\fEnter phone number");
        phoneNo = getString();
        enterIsCraftsman();
    }
    private void enterIsCraftsman(){
        System.out.println("\fEnter (1) if customer is craftsman, (2) if not");
        String isCraftsmanTemp = getString();
        while(!isCraftsmanTemp.equals("1") && !isCraftsmanTemp.equals("2")){
            System.out.println(errorMessage);
            System.out.println("\fTry again!");
            System.out.println("Enter (1) if customer is craftsman, (2) if not");
            isCraftsmanTemp = getString();
        }
        if(isCraftsmanTemp.equals("1")){
             isCraftsman = true;
        }
        else{
             isCraftsman = false;
        }
        createCustomer();
    }
    private void createCustomer(){
        Customer newCustomer = new Customer(name, address, phoneNo, cprNo, isCraftsman);
        customerController.getAllCustomers().add(newCustomer);
        System.out.println("Customer created!");
        pressEnter();
        writeCustomerMenu();
    }
    private void findCustomer(){    // Needs to be looked through...
        System.out.println("\fEnter CPR number: ");
        String cprNo1 = getString();
        int cprNo2 = Integer.parseInt(cprNo1);
        Customer check;
        String type = "";
        boolean found = false;
        if(customerController.getAllCustomers().size() != 0){
            for(Customer c: customerController.getAllCustomers()){
                if(c.getCprNo() == cprNo2 && !found){
                    System.out.println("Name: " + c.getName());
                    System.out.println("CPR number: " + c.getCprNo());
                    System.out.println("Address: " + c.getAddress());
                        if(c.getIsCraftsman() == true){
                            type = "Yes";
                        }
                        else{
                            type = "No";
                        }
                        System.out.println("Craftsman: " + type);
                        found = true;
                    }  
                if(c instanceof RegularCustomer){
                	System.out.println("Regular customer: Yes");
                }
                else{
                	System.out.println("Regular customer: No");
                }
                
            }
            if(!found)
            	System.out.println("Customer with this CPR number\ndoesn't exist in the system!");
        }
        else{
            System.out.println("There are no customers on the system!");
        }
        pressEnter();
        writeCustomerMenu();
    }
    private String choose(){
        boolean check = false;
        String actionChoice = "0";
        while(check != true){
            System.out.println("\fCustomer already exists in the system. Would you like to search for it?\nIf yes enter (1), or (2) to try again");
            actionChoice = getString();
            if(!actionChoice.equals("1") && !actionChoice.equals("2")){
                System.out.println(errorMessage);
                System.out.println("Try again: ");
            }
            else{
                check = true;                
            }
        }
        return actionChoice;
    }
    private boolean completeCpr(){
        boolean end = false;
        boolean choice = checkIfCustomerExists();
        String choiceTemp = "";
        while(choice != false){

            if(checkIfCustomerExists() == true){
                choiceTemp = choose();
            }   
            if(choiceTemp.equals("1")){
                 choice = false;
                 end = true;
            }
            if(choiceTemp.equals("2")){
                makeCustomer();
            }
        }
        return end;
    }
    private boolean checkIfCustomerExists(){
        boolean check = false;
        for(Customer c: customerController.getAllCustomers()){
            if(c.getCprNo() == cprNo){
                check = true;
            }
        }
        return check;
    }
    private void updateCustomer(){
        Customer currentCustomer = null;
        System.out.println("Enter CPR number: ");
        String cprNoTemp = getString();
        cprNo = Integer.parseInt(cprNoTemp);
        String type = "";
        if(customerController.getAllCustomers().size() != 0){
            for(Customer c: customerController.getAllCustomers()){
                if(c.getCprNo() == cprNo){
                    currentCustomer = c;
                }
            }
        }
        else{
            System.out.println("There are no customers in database!");
        }
        if(currentCustomer == null){
            System.out.println("Customer is not found!");
        }
        else{
            System.out.println("\fEnter name: ");
            name = getStringForNames();
            currentCustomer.setName(name);
            System.out.println("\fEnter address: ");
            address = getStringForNames();
            currentCustomer.setAddress(address);
            System.out.println("\fEnter phone number: ");
            phoneNo = getString();
            currentCustomer.setPhoneNo(phoneNo);
            System.out.println("\fEnter (1) if customer is craftsman, (2) if not");
            String isCraftsmanTemp = getString();
            while(!isCraftsmanTemp.equals("1") && !isCraftsmanTemp.equals("2")){
                System.out.println(errorMessage);
                System.out.println("\fTry again!");
                System.out.println("Enter (1) if customer is craftsman, (2) if not");
                isCraftsmanTemp = getString();
            }
            if(isCraftsmanTemp.equals("1")){
                currentCustomer.setIsCraftsman(true);
            }
            else{
                currentCustomer.setIsCraftsman(false);
            }
            System.out.println("Customer is updated!");
            System.out.println("New customers info: ");
            System.out.println("Name: " + currentCustomer.getName());
            System.out.println("CPR number: " + currentCustomer.getCprNo());
            System.out.println("Address: " + currentCustomer.getAddress());
            if(currentCustomer.getIsCraftsman() == true){
                type = "Yes";
            }
            else{
                type = "No";
            }
            System.out.println("Craftsman: " + type);
        }
        
        pressEnter();
        writeCustomerMenu();
    }
    /*
    private void updateCpr(){
        Customer currentCustomer = null;
        System.out.println("Enter CPR number: ");
        String cprNoTemp = getString();
        cprNo = Integer.parseInt(cprNoTemp);
        if(customerController.getAllCustomers().size() != 0){
            for(Customer c: customerController.getAllCustomers()){
                if(c.getCprNo() == cprNo){
                    currentCustomer = c;
                }
            }
            System.out.println("Enter new CPR number: ");
            cprNoTemp = getString();
            cprNo = Integer.parseInt(cprNoTemp);
            currentCustomer.setCprNo(cprNo);
        }
        else{
            System.out.println("There are no customers in database!");
        }
        System.out.println("CPR number has been updated");
        System.out.println("New CPR number is: " + cprNo);
        pressEnter();
        writeCustomerMenu();
    }
    */
    /**private void deleteCustomer(){
        System.out.println("Enter CPR number: ");
        String cprNoTemp = getString();
        cprNo = Integer.parseInt(cprNoTemp);
        if(customerController.getAllCustomers().size() != 0){
            for(Customer c: customerController.getAllCustomers()){
                if(c.getCprNo() == cprNo){
                    c.remove();
                }
            }
        }
        else{
            System.out.println("There are no customers in database!");
        }
    }
    */
    private void pressEnter(){
      
        System.out.println("Press enter to continue.");
        keyboard.nextLine(); 
    }
    private String getString(){
        String startChoice ;
        startChoice = keyboard.nextLine();
        String choice = startChoice.trim().toLowerCase();
        return choice;
    }
    private String getStringForNames(){
        String startChoice = "";
        startChoice = keyboard.nextLine();
        return startChoice;
    }
    private void updateCustomerToRegular(){
        Customer currentCustomer = null;
        boolean found=false;
        System.out.println("Enter CPR number of customer you want to promote:");
        String cprNoTemp = getString();
        cprNo = Integer.parseInt(cprNoTemp);
        if(customerController.getAllCustomers().size() != 0){
            for(Customer r: customerController.getAllCustomers()){
                if(r.getCprNo() == cprNo){
                    currentCustomer = r; 
                    System.out.println("Customer copied!");
                    found=true;
                }
            }
        }
        else{
            System.out.println("There are no customers in database!");
        }
        if(customerController.getAllCustomers().size() != 0){
            for(int i = 0; i < customerController.getAllCustomers().size(); i++){
                if(customerController.getAllCustomers().get(i).getCprNo() == cprNo){
                    customerController.getAllCustomers().remove(i); 
                    System.out.println("Old customer removed!");
                }
            }
        }
        //cast to Regular Customer
        if(currentCustomer!=null){
        int cpr;
        String  name, address, phoneNo;
        boolean isCraftsman;
        cpr=currentCustomer.getCprNo();
        name=currentCustomer.getName();
        address=currentCustomer.getAddress();
        phoneNo=currentCustomer.getPhoneNo();
        isCraftsman=currentCustomer.getIsCraftsman();
        double regDiscount = 5.0;
        Customer newRegularCustomer= new RegularCustomer(name,address,phoneNo,cpr,isCraftsman,regDiscount);
        customerController.getAllCustomers().add(newRegularCustomer);
        }
        
        //finishing the cast
        if(found)
        System.out.println("Customer has been promoted to regular customer!");
        else
        System.out.println("No customer with this CPR found");
        pressEnter();
        writeCustomerMenu();
    }
}